package com.whales.lms.rms.web.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whales.lms.rms.web.model.SoftwareVO;
import com.whales.lms.rms.web.service.SoftwareService;
import com.whales.shared.controller.GenericController;
import com.whales.shared.util.FileUtil;
import com.whales.shared.util.SessionUtil;

@Controller
@RequestMapping("/software/*")
public class SoftwareController extends GenericController<SoftwareVO, SoftwareVO, SoftwareService> {
	@RequestMapping
	public void downloadSoftware(HttpServletResponse response,SoftwareVO softwareVO) throws Exception{
		SoftwareVO softwareFile = service.findInfo(softwareVO);
		FileUtil.fileDownload(new File(softwareFile.getSvrPath()), softwareFile.getFileName(), response);
	}
	
	@RequestMapping
	public @ResponseBody String getVersion(HttpServletRequest request, SoftwareVO softwareVO) throws Exception{
		String strServerIP = request.getServerName();    // 서버 ip
		String strServerPort = Integer.toString(request.getServerPort());// 서버 port
		String serverRootUrl = "http://"+ strServerIP +":"+ strServerPort +request.getContextPath();  // Root 경로
		SessionUtil.setAttribute("SERVER_FULL_PATH", serverRootUrl);
		return service.getVersionByIp(softwareVO.getIp());
	}
}
